<?php

	abstract class Element {
		public $name = null;
		public $class = null;
		public $label = null;
		public $showLabel = null;
		public $id = null;
		public $style = null;
		public $more;
		abstract public function get();
	};
	
	class Input extends Element {
		public $type;
		public $value;
		
		public function __construct($_label,$_type,$_name,$_value='',$_class='',$_style='',$_more='',$_id='')
		{
			$this->label = $_label;
			$this->type = $_type;
			$this->name = $_name;
			$this->value = $_value;
			$this->class = $_class;
			$this->style = $_style;
			$this->more = $_more;
			$this->id = $_id;
			$this->showLabel = true;
		}
		
		public function get()
		{
			$element = ($this->type=='hidden' ? '' : ($this->showLabel ? ($this->id != '' ? ('<label for="' . $this->id . '">' . $this->label . '</label>') : $this->label) : '')) . 
                                    "<input type=\"$this->type\" name=\"$this->name\"";
			$element .= ($this->value != '' ? " value=\"$this->value\"" : '');
			$element .= ($this->class != '' ? " class=\"$this->class\"" : '');
			$element .= ($this->style != '' ? " style=\"$this->style\"" : '');
			$element .= ($this->id != '' ? " id=\"$this->id\"" : '');
			$element .= ($this->more != '' ? " $this->more" : '');
			$element .= " />\n";
			return $element;
		}
	};

	class Textarea extends Element {
		public $value;
		public $rows;
		public $cols;
		
		public function __construct($_name,$_value='',$_class='',$_style='',$_rows='', $_cols='',$_more='',$_id='')
		{
			$this->name = $_name;
			$this->value = $_value;
			$this->class = $_class;
			$this->style = $_style;
			$this->more = $_more;
			$this->id = $_id;
			$this->showLabel = true;
			$this->rows = $_rows;
			$this->cols = $_cols;
		}
		
		public function get()
		{
			$element = "<textarea name=\"$this->name\"";
			$element .= ($this->class != '' ? " class=\"$this->class\"" : '');
			$element .= ($this->rows != '' ? " rows=\"$this->rows\"" : '');
			$element .= ($this->cols != '' ? " cols=\"$this->cols\"" : '');
			$element .= ($this->style != '' ? " style=\"$this->style\"" : '');
			$element .= ($this->id != '' ? " id=\"$this->id\"" : '');
			$element .= ($this->more != '' ? " $this->more" : '');
			$element .= ">$this->value</textarea>";
			return $element;
		}
	};	
	
	class Checkbox extends Element {
		public $labelSide; //1-left, 2-right
		public $checked;
		
		public function __construct($_label,$_name,$_labelSide=2,$_checked=false,$_value='',$_class='',$_style='',$_more='',$_id='')
		{
			$this->label = $_label;
			$this->labelSide = $_labelSide;
			$this->name = $_name;
			$this->value = $_value;
			$this->class = $_class;
			$this->style = $_style;
			$this->more = $_more;
			$this->id = $_id;
			$this->showLabel = true;
			$this->checked = $_checked;
			if($this->id != '')
				$this->label = "<label for=\"$this->id\">$this->label</label>";
		}
		
		public function get()
		{
			$element = "<input type=\"checkbox\" name=\"$this->name\"";
			$element .= ($this->value != '' ? " value=\"$this->value\"" : '');
			$element .= ($this->class != '' ? " class=\"$this->class\"" : '');
			$element .= ($this->style != '' ? " style=\"$this->style\"" : '');
			$element .= ($this->id != '' ? " id=\"$this->id\"" : '');
			$element .= ($this->more != '' ? " $this->more" : '');
			$element .= ($this->checked ? 'checked' : '');
			$element .= " />\n";
			if($this->showLabel)
			{
				if($this->labelSide == 1)
					$element = $this->label . ' ' . $element;
				else
					$element .= ' ' . $this->label;
			}
			return $element;
		}
	};	

	class Radiobutton extends Element {
		public $labelSide; //1-left, 2-right
		public $checked;
		
		public function __construct($_label,$_name,$_labelSide=2,$_checked=false,$_value='',$_class='',$_style='',$_more='',$_id='')
		{
			$this->label = $_label;
			$this->labelSide = $_labelSide;
			$this->name = $_name;
			$this->value = $_value;
			$this->class = $_class;
			$this->style = $_style;
			$this->more = $_more;
			$this->id = $_id;
			$this->showLabel = true;
			$this->checked = $_checked;
			if($this->id != '')
				$this->label = "<label for=\"$this->id\">$this->label</label>";
		}
		
		public function get()
		{
			$element = "<input type=\"radio\" name=\"$this->name\"";
			$element .= ($this->value != '' ? " value=\"$this->value\"" : '');
			$element .= ($this->class != '' ? " class=\"$this->class\"" : '');
			$element .= ($this->style != '' ? " style=\"$this->style\"" : '');
			$element .= ($this->id != '' ? " id=\"$this->id\"" : '');
			$element .= ($this->more != '' ? " $this->more" : '');
			$element .= ($this->checked ? 'checked' : '');
			$element .= " />\n";
			if($this->showLabel)
			{
				if($this->labelSide == 1)
					$element = $this->label . ' ' . $element;
				else
					$element .= ' ' . $this->label;
			}
			return $element;
		}
	};	
	
	class Select extends Element {
		public $default;
		private $option_texts;
		private $option_values;
		public $vid;
		public $text;
		
		public function __construct($_label, $_options, $_name, $_default='-1', $_class='', $_style='', $_more='', $_id='', $_vid='id', $_text='text')
		{
			$this->label = $_label;
			$this->name = $_name;
			$this->default = $_default;
			$this->class = $_class;
			$this->style = $_style;
			$this->more = $_more;
			$this->id = $_id;
			$this->vid = $_vid;
			$this->text = $_text;
			$this->showLabel = true;
			
			$this->option_texts = array();
			$this->option_values = array();
			
			$this->add($_options);	
		}
		
		public function add($options)
		{
			if(!is_array($options))
				return false;
			foreach($options as $key => $value)
			{
				$this->option_values[] = $value['' . $this->vid];
				$this->option_texts[] = $value['' . $this->text];
			}
		}
		
		public function getOption($id)
		{
			if(count($this->option_values) > $id)
				return array($this->vid => $this->option_values[$id], $this->text => $this->option_texts[$id]);
			else
				return null;
		}
		
		public function remove($id_start,$id_end=-1)
		{
			if(count($this->option_values) <= $id_start)
				return false;
			if($id_end != -1 && count($this->option_values) <= $id_end)
				return false;
			if($id_start < 0 || $id_end < 0)
				return false;
			
			if($id_end == -1 || $id_start == $id_end)
			{
				unset($this->option_values[$id_start]);
				unset($this->option_texts[$id_start]);
			}
			else
			{
				if($id_start > $id_end)
				{
					for($i=$id_end;$i<=$id_start;$i++)
					{
						unset($this->option_values[$i]);
						unset($this->option_texts[$i]);
					}
				}
				else
				{
					for($i=$id_start;$i<=$id_end;$i++)
					{
						unset($this->option_values[$i]);
						unset($this->option_texts[$i]);
					}					
				}
			}
		}
		
		public function get()
		{
			$element = ($this->showLabel ? ($this->id != '' ? ('<label for="' . $this->id . '">' . $this->label . '</label>') : $this->label) : '') .  "<select name=\"$this->name\"";
			$element .= ($this->class != '' ? " class=\"$this->class\"" : '');
			$element .= ($this->style != '' ? " style=\"$this->style\"" : '');
			$element .= ($this->id != '' ? " id=\"$this->id\"" : '');
			$element .= ($this->more != '' ? " $this->more" : '');
			$element .= ">\n\t";
			
			foreach($this->option_values as $key => $value)
			{
				$element .="<option value=\"" . $value . "\"" . (is_array($this->default) ? (in_array($value,$this->default) ? " selected=\"selected\"" : '') : ($value == $this->default ? " selected=\"selected\"" : '')) . ">" . $this->option_texts[$key] . "</option>\n";
			}
			$element .="</select>";
			return $element;
		}
	};
	
	
	
	
	class Section {
		public $class;
		public $style;
		public $id;
		public $label;
		public $showLabel;
		public $more;
		private $elements;
		private $_elements;
		private $current;
		
		public function __construct($_label,$_class='',$_style='',$_more='',$_id='')
		{
			$this->label = $_label;
			$this->class = $_class;
			$this->style = $_style;
			$this->more = $_more;
			$this->id = $_id;
			$this->showLabel = true;
			$this->_elements = array();
			$this->elements = 0;
			$this->current = 0;
		}
		
		public function add($element)
		{
			$this->elements++;
			$this->current = $this->elements;
			$this->_elements[$this->current] = $element;
		}
		
		public function element()
		{
			if($this->elements > 0)
				return $this->_elements[$this->current];
			else
				return null;
		}
		
		public function changeElement($nr)
		{
			if($nr <= $this->elements && $nr > 0)
				$this->current = $nr;			
		}
		
		public function get()
		{
			$section = "<fieldset";
			$section .= ($this->class != '' ? " class=\"$this->class\"" : '');
			$section .= ($this->style != '' ? " style=\"$this->style\"" : '');
			$section .= ($this->id != '' ? " id=\"$this->id\"" : '');
			$section .= ($this->more != '' ? " $this->more" : '');
			$section .= ">\n";		
			$section .= ($this->showLabel ? "\t<legend>$this->label</legend>\n" : '\t');
			
			foreach($this->_elements as $key => $element)
			{
				if(get_class($element) == 'Input' && $element->type == 'hidden') {
					$section .= $element->get() . "\n";
                                } else {
					$section .= $element->get() . "<br />\n";
                                }
			}
			$section .= "</fieldset>";
			return $section;
		}
	};


	class Form extends Element {
		private $sections; //number of sections
		private $_sections; //section objects
		private $current; //current section
		
		public $action;
		public $method;
		public $onsubmit;
		public $submit_pos;
		private $buttons;
		
		public function __construct($_action,$_method,$_class='', $_style='', $_more='', $_id='', $_onsubmit='')
		{
			$this->action = $_action;
			$this->method = $_method;
			$this->class = $_class;
			$this->style = $_style;
			$this->more = $_more;
			$this->id = $_id;
			$this->showLabel = false;
			$this->label = '';
			$this->onsubmit = $_onsubmit;
			$this->buttons = array();
			
			$this->_sections = array();
			$this->sections = 0;
			$this->current = 0;
		}
		
		public function changeSection($nr)
		{
			if($nr <= $this->sections && $nr > 0)
				$this->current = $nr;
		}
		
		public function section()
		{
			if($this->current > 0)
				return $this->_sections[$this->current];
			else
				return null;
		}
		
		public function addSection($_label,$_class='',$_style='',$_more='',$_id='')
		{
			$this->sections++;
			$this->current = $this->sections;
			$this->_sections[$this->current] = new section($_label,$_class,$_style,$_more,$_id);
			$this->buttons[$this->current] = array();
		}
		
		public function addButton($button, $where=-1)
		{
			if($where == -1)
				$where = $this->current;
			$this->buttons[(int)$where][] = $button;
		}
		
		
		public function get()
		{
			$f = "<form action=\"$this->action\" method=\"$this->method\"";
			$f .= ($this->class != '' ? " class=\"$this->class\"" : '');
			$f .= ($this->style != '' ? " style=\"$this->style\"" : '');
			$f .= ($this->id != '' ? " id=\"$this->id\"" : '');
			$f .= ($this->more != '' ? " $this->more" : '');
			$f .= ($this->onsubmit != '' ? " onSubmit=\"$this->onsubmit\"" : '');
			$f .= ">\n";
			foreach($this->_sections as $key => $section)
			{
				$f .= $section->get();
				foreach($this->buttons[$key] as $key => $button)
					$f .= $button->get();
			}

			$f .= "</form>";
			return $f;
		}
	};
	
	class Row{
		public $row;
		
		public function __construct($arr=array())
		{
			$this->row = $arr;
		}
		
		public function add($elem)
		{
			$this->row[] = $elem;
		}
		
		public function get()
		{
			$ret = '';
			foreach($this->row as $key=> $elem)
			{
				if(!is_string($elem))
					$ret .= $elem->get();
				else
					$ret .= $elem;
			}
			return $ret;
		}
	};

	class PureHtml{
		public $html;
		
		public function __construct($text)
		{
			$this->html = $text;
		}
		
		public function get()
		{
			return $this->html;
		}
	};
	
	
?>